/*
 * Copyright (c) 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.libermundi.theorcs.core.tapestry.services.assets;

import java.io.File;
import java.util.Map;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.internal.services.AbstractAsset;
import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.services.AssetFactory;
import org.apache.tapestry5.services.Context;
import org.apache.tapestry5.services.InvalidationListener;
import org.apache.tapestry5.services.Request;

public class OfsAssetFactory implements AssetFactory, InvalidationListener {
	private static final String FILE_SEPARATOR = System.getProperty("file.separator");
	
	public static final String TEMP_FOLDER = "file:///" + System.getProperty("java.io.tmpdir") + FILE_SEPARATOR + "TheORCS"  + FILE_SEPARATOR;
	
	public static final String OFS_RESOURCE_FOLDER="resources" + FILE_SEPARATOR;
	
	public static final String DEFAULT_CLASSPATH_LOCATION="org" + FILE_SEPARATOR + "libermundi" + FILE_SEPARATOR + "theorcs"  + FILE_SEPARATOR;
    
	public static final String DEFAULT_CONTEXT_LOCATION="WEB-INF" + FILE_SEPARATOR + "resources" + FILE_SEPARATOR;
    
	public static final String OFS_VIRTUAL_FOLDER = "ofs";
	
	private final String _ofsAssetPrefix;

	private final Context _context;
    private final Request _request;
    
    private final Map<Resource, String> _resourcePathMap = CollectionFactory.newConcurrentMap();
    
    private final OfsResource _rootResource;
    private final boolean _invariant;
    private final String _localInstallRootDir;
    
    public OfsAssetFactory(Request request, Context context, String localInstallRootDir, String t5AssetPathPrefix) {
        _context = context;
        _request = request;
        _invariant = false;
        _localInstallRootDir=sanitiseDirPath(localInstallRootDir);
        _rootResource=new OfsResource(_context, _localInstallRootDir, "");
        _ofsAssetPrefix = t5AssetPathPrefix + OFS_VIRTUAL_FOLDER + "/";
    }
    
    public String getLocalInstallRootDir(){
    	return _localInstallRootDir;
    }
    
    /* (non-Javadoc)
     * @see org.apache.tapestry5.services.AssetFactory#createAsset(org.apache.tapestry5.ioc.Resource)
     */
    @Override
    public Asset createAsset(final Resource resource) {
    	return new AbstractAsset(_invariant) {
    	    @Override
            public Resource getResource() {
                return resource;
            }

            @Override
            public String toClientURL() {
            	return _request.getContextPath()  + _ofsAssetPrefix + convertPath(resource.getPath());
            }
        };
    }
    
    /* (non-Javadoc)
     * @see org.apache.tapestry5.services.AssetFactory#getRootResource()
     */
    @Override
    public Resource getRootResource() {
        return _rootResource;
    }
    
    /* (non-Javadoc)
     * @see org.apache.tapestry5.services.InvalidationListener#objectWasInvalidated()
     */
    @Override
    public void objectWasInvalidated() {
        _resourcePathMap.clear();        
    }
    
	private static String sanitiseDirPath(String dirPath) {
		String path = dirPath.trim();
		
		if(path.startsWith("classpath")){
			path=TEMP_FOLDER;
		}

		if (!path.endsWith(FILE_SEPARATOR)) {
			path += FILE_SEPARATOR;
		}

		File dir = new File(path);

		if (!dir.exists()) {
			dir.mkdirs();
		}

		return path;
	}
	
	private static String convertPath(String path){
		return path.replaceAll("\\\\", "/") ;
	}

}
